"""
@Author    : ghenyar
@Time      : 2025/9/8 10:46
@File      : role
@Desc      : 角色路由
"""
from functools import partial

from fastapi import APIRouter, Security, Request, Query,Depends
from sqlalchemy.ext.asyncio import AsyncSession

from app.admin.decorator import log_operation
from app.admin.schemas.role import RoleSchema
from app.admin.security import authority
from app.admin.service.role_service import RoleService
from app.common.response import ApiResponse
from app.core.db import async_get_db

router = APIRouter()
title = "角色"
# 鉴权
Auth = partial(Security, authority)


@router.get("/lists", dependencies=[Auth(scopes=["role:list"])], response_model=ApiResponse)
async def role_lists(page: int = Query(1), pageSize: int = Query(10), key: str = Query(None),db: AsyncSession = Depends(async_get_db)):
    """
    角色列表
    :param page: 分页
    :param pageSize: 每页显示条数
    :param key: 关键字
    :param db: 数据库会话
    :return:
    """
    result = await RoleService.lists_data(db,page, pageSize, key)
    return ApiResponse.page_list(result)


@router.post("/save", dependencies=[Auth(scopes=["role:save"])], response_model=ApiResponse)
@log_operation(log_message=title, types=1)
async def save_role(request: Request, role_data: RoleSchema,db: AsyncSession = Depends(async_get_db)):
    """
    新增角色
    :param request: 请求体
    :param role_data: 角色数据
    :param db: 数据库会话
    :return:
    """
    result = await RoleService.create_or_update(db,role_data)
    msg = "新增角色"
    if result:
        return ApiResponse.success(f"{msg}成功")
    else:
        return ApiResponse.failed(f"{msg}失败！")


@router.put("/save", dependencies=[Auth(scopes=["role:update"])], response_model=ApiResponse)
@log_operation(log_message=title, types=2)
async def update_role(request: Request, role_data: RoleSchema,db: AsyncSession = Depends(async_get_db)):
    """
    编辑角色
    :param request: 请求体
    :param role_data: 角色数据
    :param db: 数据库会话
    :return:
    """
    result = await RoleService.create_or_update(db,role_data)
    msg = "编辑角色"
    if result:
        return ApiResponse.success(f"{msg}成功")
    else:
        return ApiResponse.failed(f"{msg}失败！")


@router.delete("/delete/{r_id}", dependencies=[Auth(scopes=["role:delete"])], response_model=ApiResponse)
@log_operation(log_message=title, types=3)
async def delete_role(request: Request, r_id: int,db: AsyncSession = Depends(async_get_db)):
    """
    删除角色
    :param request: 请求体
    :param r_id: 角色id
    :param db: 数据库会话
    :return:
    """
    role = RoleService()
    msg = "删除角色"
    if await role.check_bound_role(db,r_id):
        return ApiResponse.failed("无法删除！当前角色已绑定管理员")

    role_del = await role.del_role(db,r_id)
    if role_del:
        return ApiResponse.success(f"{msg}成功")
    else:
        return ApiResponse.failed(f"{msg}失败！")

@router.get("/get_role",response_model=ApiResponse)
async def get_role_options(db: AsyncSession = Depends(async_get_db)):
    """
    为下拉框
    :param db: 数据库会话
    :return:
    """
    result = await RoleService.get_role_for_select(db)
    return ApiResponse.result(result)
